package com.zzf.uibasetpractice

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MsgAdapter(val msgList:List<Msg>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

//    inner class LeftViewHolder(view:View) : RecyclerView.ViewHolder(view){
//        val leftMsg:TextView = view.findViewById(R.id.leftMsg)
//    }
//
//    inner class RightViewHolder(view:View) : RecyclerView.ViewHolder(view){
//        val rightMsg:TextView = view.findViewById(R.id.rightMsg)
//    }

    override fun getItemViewType(position: Int): Int {
        val msg = msgList[position]
        return msg.type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == Msg.TYPE_RECEIVED){
            val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item,parent,false)
//            return LeftViewHolder(view)
            return MsgViewHolder.LeftViewHolder(view)
        }else{

            val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item,parent,false)
//            return RightViewHolder(view)
            return MsgViewHolder.RightViewHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val msg = msgList[position]
//        when(holder){
//            is LeftViewHolder -> holder.leftMsg.text = msg.content
//            is RightViewHolder -> holder.rightMsg.text = msg.content
//        }

        when(holder){
            is MsgViewHolder.LeftViewHolder -> holder.leftMsg.text = msg.content
            is MsgViewHolder.RightViewHolder -> holder.rightMsg.text = msg.content
        }

    }

    override fun getItemCount(): Int {
        return msgList.size
    }

}